/**
 * ****************************************************************************
 * Project: RoboBridge
 * ----------------------------------------------------------------------------
 * -------------------------- [c]2012 - KrawallMann ---------------------------
 * ---------------------- http://www.krawallmann.de.tc/ -----------------------
 * ****************************************************************************
 * File: DEVICE_ID_CMPS03.java Package:
 * tc.de.krawallmann.robobridge.devices.implementation Version: 1.0 Author(s):
 * KrawallMann
 * ****************************************************************************
 * Description:
 * ****************************************************************************
 */
package tc.de.krawallmann.robobridge.devices.implementation;

import tc.de.krawallmann.robobridge.communication.Command;
import tc.de.krawallmann.robobridge.devices.AbstractDevice;
import tc.de.krawallmann.robobridge.devices.CompassSensor;
import tc.de.krawallmann.robobridge.devices.Robot;
import tc.de.krawallmann.robobridge.util.BitOperations;

/**
 * The Class DEVICE_ID_CMPS03 represents a digital compass sensor made by Devantech.
 * 
 * @author KrawallMann
 * @version 1.0
 */
public class CMPS03 extends AbstractDevice implements CompassSensor {

	public static final String CMPS03_ANGLE = "CMPS03_ANGLE";
	/** The angle. */
	private transient int angle = 0;

	/**
	 * Instantiates a new DEVICE_ID_CMPS03.
	 * 
	 * @param robot
	 *            the robot
	 * @param deviceId
	 *            the device id
	 */
	public CMPS03(final Robot robot, final int deviceId) {
		super(robot, AbstractDevice.DEVICE_ID_CMPS03, deviceId, "Devantech", "DEVICE_ID_CMPS03");
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * tc.de.krawallmann.robobridge.devices.implementation.CompassSensor#getAngle
	 * ()
	 */
	@Override
	public int getAngle() {
		return angle;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * basepackage.IncomingCommandListener#onCommandReceived(basepackage.Command
	 * )
	 */
	@Override
	public boolean onCommandReceived(final Command command) {
		if (containsRelevantData(command, Integer.class, Integer.class)) {
			final int newAngle =
					BitOperations.combineHighLow((Integer) command.getParameter(1),
							(Integer) command.getParameter(2));
			firePropertyChange(CMPS03_ANGLE, angle, newAngle);
			angle = newAngle;
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see tc.de.krawallmann.robobridge.devices.AbstractDevice#toString()
	 */
	@Override
	public String toString() {
		return "DEVICE_ID_CMPS03 [deviceId=" + deviceId + ", manufacturer=" + manufacturer + ", model="
				+ model + ", angle=" + angle + "]";
	}

}
